package sg.edu.nus.iss.vmcs.customer;

import java.awt.*; //import java.awt.event.*;
import java.util.Observable;

//import sg.edu.nus.iss.vmcs.system.SimulatorControlPanel;
import sg.edu.nus.iss.vmcs.payment.CoinReceiver;
import sg.edu.nus.iss.vmcs.system.Fault;
import sg.edu.nus.iss.vmcs.system.FaultObserver;
import sg.edu.nus.iss.vmcs.system.FaultType;
import sg.edu.nus.iss.vmcs.util.LabelledDisplay;
import sg.edu.nus.iss.vmcs.util.Log;
import sg.edu.nus.iss.vmcs.util.WarningDisplay;

public class CustomerPanel extends Dialog  {

	private static final String TITLE = "VMCS - Customer Panel";
	private static final String HEADING = "VIMTO Soft Drink Dispenser";

	private TransactionController transactionCtrl;
	private Button terminateButton;
//	private WarningDisplay noChangeAvailableDisplay;
//	private WarningDisplay falseCoin;
//	private LabelledDisplay totalMoneyInsertedDisplay;
//	private LabelledDisplay refundChangeTrayDisplay;
	private LabelledDisplay canCollectionBox;
//	private CoinInputBox coinInputBox;
	private DrinkSelectionBox drinkSelectionBox;

	public CustomerPanel(Frame fr, TransactionController controller) {
		super(fr, TITLE, false);
//		observeFault(Fault.getInstance());
		this.transactionCtrl = controller;

		Panel northPanel = new Panel();
		northPanel.setLayout(new GridLayout(0, 1));

		Label heading = new Label(HEADING);
		heading.setFont(new Font("Helvetica", Font.BOLD, 24));
		northPanel.add(heading);

//		coinInputBox = new CoinInputBox(northPanel, transactionCtrl);
//		northPanel.add(coinInputBox);
//
//		falseCoin = new WarningDisplay("        Invalid Coin        ");
//		northPanel.add(falseCoin);

		// Total money inserted display
//		totalMoneyInsertedDisplay = new LabelledDisplay("Total Money Insert:",
//				5, LabelledDisplay.FLOW);
//		totalMoneyInsertedDisplay.setEditable(false);
//		totalMoneyInsertedDisplay.setValue("0 c");
//		northPanel.add(totalMoneyInsertedDisplay);

		/**
		 * Center
		 */
		Panel centerPanel = new Panel();
		centerPanel.setLayout(new GridLayout(0, 1));

		// Drink selection box
		drinkSelectionBox = new DrinkSelectionBox(northPanel, transactionCtrl);
		centerPanel.add(drinkSelectionBox);

		/**
		 * South part
		 */
		Panel southPanel = new Panel();
		southPanel.setLayout(new GridLayout(0, 1));

		// No change display warning display
//		noChangeAvailableDisplay = new WarningDisplay("No Change Available");
//		southPanel.add(noChangeAvailableDisplay);

		// Terminate button
		Panel terminateButtonPanel = new Panel();
		terminateButtonPanel.setLayout(new FlowLayout());
		terminateButton = new Button("Terminate and Return Cash"); 
		TerminateButtonListener tbl = new TerminateButtonListener(
				this.transactionCtrl);
		// terminateButton.setEnabled(false);
		terminateButton.addActionListener(tbl);
		terminateButtonPanel.add(terminateButton);
		southPanel.add(terminateButtonPanel);

		// Refund/change collection display
//		refundChangeTrayDisplay = new LabelledDisplay("Collect Coins:", 10,
//				LabelledDisplay.FLOW);
//		refundChangeTrayDisplay.setEditable(false);
//		refundChangeTrayDisplay.setValue("0 c");
//		southPanel.add(refundChangeTrayDisplay);

		// Can collection display
		canCollectionBox = new LabelledDisplay("Collect Can Here:", 10,
				LabelledDisplay.FLOW);

		canCollectionBox.setEditable(false);
		canCollectionBox.setValue("NO CAN");

		/*
		 * Add parts to dialog box
		 */
		this.setLayout(new BorderLayout());
		this.add("North", northPanel);
		this.add("Center", centerPanel);
		this.add("South", southPanel);

		pack();
		setLocation(20, 230);

	}

	public void closeDown() {
		this.dispose();
	}

	public void display() {
		this.setVisible(true);
	}


	public void setValue_can_collection_box(String s) {
		canCollectionBox.setValue(s);
	}

	public DrinkSelectionBox getDrinkSelectionBox() {
		return drinkSelectionBox;
	}

	public void setStateTerminateButton(boolean b) {
		terminateButton.setEnabled(b);
	}

//	public void observeFault(Fault fault) {
//		Log.LogIt("CustomerPanel.ObserveFault:" + fault.getState());
//		fault.addObserver(this);		
//	}
//
//	public void update(Observable arg0, Object arg1) {
//		try {
//			Fault fault = (Fault) arg0;
//			Log.LogIt("CustomerPanel.update:" + fault.getState());
////			showFault(fault);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}		
//	}
}
